package com.ruoyi.project.modules.domainInfo.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.domainInfo.domain.DomainInfo;
import com.ruoyi.project.modules.domainInfo.service.IDomainInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 归属域信息Controller
 *
 * @author smallrain
 * @date 2022-03-26
 */
@Controller
@RequestMapping("/domainInfo/domainInfo")
public class DomainInfoController extends BaseController {
    private String prefix = "domainInfo/domainInfo";

    @Autowired
    private IDomainInfoService domainInfoService;

    @RequiresPermissions("domainInfo:domainInfo:view")
    @GetMapping()
    public String domainInfo() {
        return prefix + "/domainInfo";
    }

    /**
     * 查询归属域信息列表
     */
    @RequiresPermissions("domainInfo:domainInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DomainInfo domainInfo) {
        startPage();
        List<DomainInfo> list = domainInfoService.selectDomainInfoList(domainInfo);
        return getDataTable(list);
    }

    /**
     * 导出归属域信息列表
     */
    @RequiresPermissions("domainInfo:domainInfo:export")
    @Log(title = "归属域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DomainInfo domainInfo) {
        List<DomainInfo> list = domainInfoService.selectDomainInfoList(domainInfo);
        ExcelUtil<DomainInfo> util = new ExcelUtil<DomainInfo>(DomainInfo.class);
        return util.exportExcel(list, "归属域信息数据");
    }

    /**
     * 新增归属域信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存归属域信息
     */
    @RequiresPermissions("domainInfo:domainInfo:add")
    @Log(title = "归属域信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DomainInfo domainInfo) {
        return toAjax(domainInfoService.insertDomainInfo(domainInfo));
    }

    /**
     * 修改归属域信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DomainInfo domainInfo = domainInfoService.selectDomainInfoById(id);
        mmap.put("domainInfo", domainInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存归属域信息
     */
    @RequiresPermissions("domainInfo:domainInfo:edit")
    @Log(title = "归属域信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DomainInfo domainInfo) {
        return toAjax(domainInfoService.updateDomainInfo(domainInfo));
    }

    @PostMapping("/domainObjectList")
    @ResponseBody
    public AjaxResult domainObjectList(Long id) {
        DomainInfo domainInfo = this.domainInfoService.selectDomainInfoById(id);
        if (domainInfo != null) {
            if (domainInfo.getDomainObjectInfoList() != null) {
                return AjaxResult.success(domainInfo.getDomainObjectInfoList());
            }
        }
        return AjaxResult.success(new ArrayList<>());
    }

    /**
     * 删除归属域信息
     */
    @RequiresPermissions("domainInfo:domainInfo:remove")
    @Log(title = "归属域信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(domainInfoService.deleteDomainInfoByIds(ids));
    }
}
